Fix duplicate fields and buttons in the inspector when using inheritance #16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The following pull request provides a solution to the issue of duplicated fields in the inspector when using inheritance (#15 and partially #12). The cause of the problem is the concatenation of the members of the current type and its base types.
Solution
The proposed solution is to use
Distinct
to remove duplicates, using bothMemberInfo.Name
andMemberInfo.DeclaringType
as the equality check:The same logic was applied to
GetAllFieldsIncludingBaseNonPublic
,GetAllPropertiesIncludingBaseNonPublic
andGetAllMethodsIncludingBaseNonPublic
.I've decided to use
Reverse
operator too, to order the fields from the base types first, then the fields from the current type - same as the default Unity inspector does.Tests
Recreated the example from #13, adding private methods of same name too:
Inspector of a
C
instance:#15 seems to be caused by two different issues, as its still not fixed. The following script:
...still isn't displayed correctly in the inspector (compare to the example in the issue):
I'll update the issue, if the pull request is accepted.
Other solutions considered
Concat
in the original implementation withUnion
, leaving theGetAll...IncludingBaseNonPublic
recursive. This would cause theUnion
to be evaluated for each level of the hierarchy, resulting in redundant equality checks.Type.Name
alone as the equality check. This would affect types that hide inherited members or redeclare private members of base types, returning only the member closest to the current type. Same goes for methods - only the closest method to the current type would get a button when usingButtonAttribute
. In the example above, onlyA.TestPrivate
would be visible in the inspector of aC
instance.